package com.clstu.frame;

import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

/**
 * 该类用于测试动作事件的监听, 就是鼠标按下按键这些....
 */
public class Action2 extends JFrame {
    static JButton red = new JButton("红色");
    static JButton yellow = new JButton("黄色");
    static JButton blue = new JButton("蓝色");
    static JPanel jPanel = new JPanel();
    static JLabel jLabel = new JLabel("请单击下面按钮");

    public Action2() throws HeadlessException {
        super("动作事件监听");
        setBounds(10,20,420,400);
        jLabel.setOpaque(true);//设置是否透明 true表示不透明
        jLabel.setHorizontalAlignment(JLabel.CENTER);
        add(jLabel,"Center");
        jPanel.add(red);
        jPanel.add(yellow);
        jPanel.add(blue);
        add(jPanel,"South");
        B b = new B();
        red.addActionListener(b);
        blue.addActionListener(b);
        //yellow.addActionListener(b);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
         new Action2();
    }

    private class B implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == Action2.red){
                Action2.red.setText("按下的是红色按钮");
                Action2.red.setBackground(Color.red);
            }
            if(e.getSource() == Action2.yellow){
                Action2.yellow.setText("按下的是黄色按钮");
                Action2.yellow.setBackground(Color.yellow);
            }
            if(e.getSource() == Action2.blue){
                Action2.blue.setText("按下的是蓝色按钮");
                Action2.blue.setBackground(Color.blue);
            }
        }
    }

}
